import time

class ChannelManager():
    def __init__(self, irc):
        self.irc = irc
        self.channels = {}
    def joining(self, channel):
        ch = Channel(channel)
        self.channels[channel] = ch
        return ch

    def parting(self, channel):
        del self.channels[channel]

    def get(self, channel_name):
        if channel_name in self.channels.keys():
            return self.channels[channel_name]

        """silently fail -- should be garbage collected"""
        return Channel('null')

class Channel():
    def __init__(self, channel_name):
        self.users = []
        self.time_joined = time.time()
        self.topic = ""
        self.topic_time = ""
        self.topic_setter = ""
        self.cmodes = {}
        self.getting_names = False
        self.channel = channel_name
        self.users_voice = []
        self.users_op = []
    def __repr__(self):
        return "<Channel %s: %s %s %s>" % (self.channel, self.topic, self.topic_time, self.topic_setter) 

    def __str__(self):
        return "Channel %s: %s" % (self.channel, self.users)

    def get_names(self, sender, data, purge=False):
        if purge:
            self.users = []

        if self.getting_names:
            for name in data.split():
                if name[0] == "@":
                    name = name[1:]
                    self.add_name(name,"op")
                elif name[0] == "+":
                    name = name[1:]
                    self.add_name(name,"voice")
                self.add_name(name)

    def start_names(self):
        self.getting_names = True
    def add_name(self, name, mode=""):
        if name not in self.users:
            self.users.append(name)
        if mode == "voice":
            if name not in self.users_voice:
                self.users_voice.append(name)
        elif mode == "op":
            if name not in self.users_op:
                self.users_op.append(name)
    def del_names(self, name):
        for list in self.users,self.users_voice,self.users_op:
            if name in list:
                list.remove(name)
    def change_mode(self, mode, data, type):
        if type == "+":
            if mode == "o":
                self.add_op(data)
            elif mode == "v":
                self.add_voice(data)
            else:
                self.cmodes[mode] = data
        else:
            try:
                if mode == "o":
                    self.del_op(data)
                elif mode == "v":
                    self.del_voice(data)
                else:
                    del self.cmodes[mode]
            except:
                pass
    def add_op(self, name):
        if name not in self.users_op:
            self.users_op.append(name)
    def add_voice(self, name):
        if name not in self.users_voice:
            self.users_op.append(name)
    def del_op(self, name):
        if name in self.users_op:
            self.users_op.remove(name)
    def del_voice(self, name):
        if name in self.users_voice:
            self.users_voice.remove(name)
    def end_names(self):
        self.getting_names = False
    def isop(self, name):
        return name in self.users_op
    def isvoice(self, name):
        return name in self.users_voice
